package org.laowenchao.server;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.laowenchao.dao.BaseDao;
import org.laowenchao.dao.CellTowerInfoDao;
import org.laowenchao.pojo.CellTowerInfo;
import org.laowenchao.pojo.MyGSMPosition;

public class DataReviseServlet extends HttpServlet {

	
	private static final long serialVersionUID = 2225959701375610485L;
	final static long TOWER_LASTS_TIME =604800000;//7*24*60*60*1000 ;
	final static int MAX_LIST_SIZE = 100;
	static LinkedList<String> writableCellList = new LinkedList<String>();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String key = req.getParameter("k");

		System.out.println(key);
		
		if("record".equals(key))
		{
			String cellId = req.getParameter("cellid");
			String lac = req.getParameter("lac");
			String time = req.getParameter("time");
			System.out.println("cellid");
			if(Integer.parseInt(cellId)>0)
			{
				MyGSMPosition position = new MyGSMPosition(cellId,lac,new Date(Long.parseLong(time)));
				boolean b = new BaseDao().addObject(position);
				CellTowerInfoDao cellDao = new CellTowerInfoDao();		
				CellTowerInfo towerInfo=cellDao.getInfo(cellId, lac);
				if(towerInfo==null)
				{
					resp.getWriter().write(0);
					writableCellList.addFirst(cellId);
					if(writableCellList.size()>MAX_LIST_SIZE)
						writableCellList.removeLast();
					
				}
				else
				{
					resp.getWriter().write(1);	
				}

			}
		}	
		else if("refresh".equals(key))
		{	
			String cellId = req.getParameter("cellid");
			String lac = req.getParameter("lac");
			String lat = req.getParameter("lat");
			String lon = req.getParameter("lon");
			String p = req.getParameter("p");
			String time = req.getParameter("time");
			
			
			
			p = URLDecoder.decode(p,"UTF-8"); 
			
			if(Integer.parseInt(cellId)>0&&writableCellList.contains(cellId))
			{
				//System.out.println("!!!"+writableCellList.size());
				writableCellList.remove(cellId);
				CellTowerInfoDao cellDao = new CellTowerInfoDao();	
				CellTowerInfo oldTowerInfo=cellDao.getInfo(cellId, lac);
				if(oldTowerInfo==null)
				{
					CellTowerInfo towerInfo = new CellTowerInfo(cellId,lac,lat,lon,p,new Date(Long.parseLong(time)));
					boolean b = new BaseDao().addObject(towerInfo);
					if(b)
						resp.getWriter().write(1);
					else
						resp.getWriter().write(0);
					
				}
				else
				{
					oldTowerInfo.setLat(lat);
					oldTowerInfo.setLon(lon);
					oldTowerInfo.setPosition(p);
					oldTowerInfo.setRefreshTime(new Date(Long.parseLong(time)));
					boolean b = new BaseDao().updateObject(oldTowerInfo);
				}				
			}
			
		}
	}
}
